package com.westa.smallshop;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.westa.smallshop.util.FakeCrashLibrary;
import com.westa.smallshop.util.TokenUtil;
import com.westa.smallshop.util.volley.MyHttpClient;
import com.westa.smallshop.util.volley.VolleySingleton;

import org.acra.ACRA;

import timber.log.Timber;

/**
 * Created by dune on 15/12/14.
 */
//@ReportsCrashes(
//        formUri = "http://api.qducc.com/api/crash/android",
//        mode = ReportingInteractionMode.TOAST,
//        forceCloseDialogAfterToast = false, // optional, default false
//        resToastText = R.string.crash_toast_text)
public class SmallShopApplication extends Application {


    private RefWatcher refWatcher;

    public static RefWatcher getRefWatcher(Context context) {
        SmallShopApplication application = (SmallShopApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initData();
    }

    private void initData() {

        refWatcher = LeakCanary.install(this);
        ACRA.init(this);
//        initUMengSetting();
        TokenUtil.initToken(getApplicationContext());
        MyHttpClient.init();
        VolleySingleton.createInstance(getApplicationContext());
        initTimber();
        SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID +"=58207260");
    }

//    private void initUMengSetting() {
//
//        PlatformConfig.setWeixin("wx268b4527986bfbee", "5bfae846021a56cb9e39f1bd42e04645");
//    }



    private void initTimber() {

        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        } else {
            Timber.plant(new CrashReportingTree());
        }
    }

    /**
     * A tree which logs important information for crash reporting.
     */
    private static class CrashReportingTree extends Timber.Tree {
        @Override
        protected void log(int priority, String tag, String message, Throwable t) {
            if (priority == Log.VERBOSE || priority == Log.DEBUG) {
                return;
            }
            FakeCrashLibrary.log(priority, tag, message);
            if (t != null) {
                if (priority == Log.ERROR) {
                    FakeCrashLibrary.logError(t);
                } else if (priority == Log.WARN) {
                    FakeCrashLibrary.logWarning(t);
                }
            }
        }
    }
}
